$(function(){
	$("#"+tabId+" #search_png").click(function(){
		var dateFrom = $("input#startDate").val();
		var dateTo = $("input#endDate").val();
		if(new Date(dateFrom.replace(/-/g,"/"))>new Date(dateTo.replace(/-/g,"/"))){
			drgUI.warning("选择的时间段不正确，请重新选择") ;
			return ;
		}		
		getDepWeekMonitorData();
	})
	//添加加载效果	
	$.post(contextPath + "/DailyRecord/getLastestIssue.dhc",
		{'hospCode':paramTabPage[tabId].hospCode}).done(function(result) {
		if(result.success && result.data && result.data.length>0){
			var date= result.data;
			var arr = date.split('-');
			$("input#endDate").val(date);
			$("input#startDate").val(arr[0] +"-"+ arr[1] +"-01");
			getDepWeekMonitorData();
		}else{
			drgUI.warning('无日监测数据、出院日期为空、院区编码为空！');
		}
	});
});

function getDepWeekMonitorData(){
	var dateFrom = $("input#startDate").val();
	var dateTo = $("input#endDate").val();
	var yearFrom = dateFrom.substring(0,4);
	var yearTo = dateTo.substring(0,4);
	$('#'+tabId+' #startTime').html(dateFrom);
	$('#'+tabId+' #endTime').html(dateTo);
	$('#'+tabId+' #monitorEndTime').html(dateTo);	
	var roleName = "";
	var depCode = "";	
	$.ajax({
		type:"POST",
		url:contextPath+"/DailyRecord/getRole.dhc",
		dataType:"json",
		async:false,
		success:function(result){
			roleName = result.roleName;
			depCode = result.depCode;
			$.post(contextPath+'/DailyRecord/getDepOnlyShow.dhc',{
				'fromDate':dateFrom,
				'toDate':dateTo,
				'depCode':depCode
		 		}).done(function(result){
		 			$('#'+tabId+' .depName').html(result.data[0].DEP_NAME);//科室名称
		 			$('#'+tabId+' #depGroupName').html(result.data[0].DEPGROUP_DESC);//科室组名称 					 			
		 			$('#'+tabId+' .avgCost').html(result.data[0].AVGCOST==null?"-":result.data[0].AVGCOST);//次均费用本期值
		 			var targetDiff = "";
		 			if(result.data[0].AVGCOST!=null&&result.data[0].BAVGCOST!=null){
		       			 var diff = (result.data[0].AVGCOST-result.data[0].BAVGCOST).toFixed(2);
		       			 if(diff>0)
		       				targetDiff = '<span style="color:red;font-weight:bold;">'+diff+'</span>';
		       			 else
		       				targetDiff = diff; 
		  			 } else{
		       			targetDiff = '<span>-</span>';
		  			 }
		 			$('#'+tabId+' #avgCostDiffBenchmark').html(targetDiff);//次均费用目标值差距		 			
		 			$('#'+tabId+' #avgCostBenchmark').html(result.data[0].BAVGCOST==null?"-":result.data[0].BAVGCOST);//次均费用目标值		 			
		 			var sumDiff = "";
		 			if(result.data[0].AVGCOST!=null&&result.data[0].SUM_AVGCOST!=null){
		       			 var diff = (result.data[0].AVGCOST-result.data[0].SUM_AVGCOST).toFixed(2);
		       			 if(diff>0)
		       				sumDiff = '<span style="color:red;font-weight:bold;">'+diff+'</span>';
		       			 else
		       				sumDiff = diff; 
		  			 }else{
		       			sumDiff = '<span>-</span>';
		  			 }
		 			$('#'+tabId+' #avgCostDiffSum').html(sumDiff);//次均费用累计值差距
		 			$('#'+tabId+' #avgCostSum').html(result.data[0].SUM_AVGCOST==null?"-":result.data[0].SUM_AVGCOST);//次均费用累计值
		 					 			
		 			var spGrow = "";
		 			if(result.data[0].AVGCOST!=null&&result.data[0].QUOTA_DETAIL_SUM!=null&&result.data[0].QUOTA_DETAIL_SUM!=0){//QUOTA_DETAIL_SUM 次均费用去年同期值
			   			 var diff = (((result.data[0].AVGCOST-result.data[0].QUOTA_DETAIL_SUM)/result.data[0].QUOTA_DETAIL_SUM)*100).toFixed(2)+"%";
			   			 if(diff>0)
			   				spGrow = '<span style="color:red;font-weight:bold;">'+diff+'</span>';
			   			 else
			   				spGrow = diff; 
					 }else{
			   			spGrow = '<span>-</span>';
					 }
		 			$('#'+tabId+' #avgCostDiffGrow').html(spGrow);//次均费用同期增长
		 			$('#'+tabId+' #avgCostSameAdd').html(result.data[0].QUOTA_DETAIL_SUM==null?"-":result.data[0].QUOTA_DETAIL_SUM);//次均费用同期累计值
		 			
		 			
		 			$('#'+tabId+' #outCase').html(result.data[0].OUTSUM==null?"-":result.data[0].OUTSUM);//出院病例数-本期值
		 			$('#'+tabId+' #outCaseSum').html(result.data[0].SUM_OUTSUM==null?"-":result.data[0].SUM_OUTSUM);//出院病例数-累计值
		 			
		 			$('#'+tabId+' #avgDays').html(result.data[0].AVGBEDDAYS==null?"-":result.data[0].AVGBEDDAYS);//平均住院日-本期值
		 			$('#'+tabId+' #avgDaysBenchmark').html(result.data[0].BAVGDAYS==null?"-":result.data[0].BAVGDAYS);//平均住院日-目标值
		 			$('#'+tabId+' #avgDaysSum').html(result.data[0].SUM_AVGBEDDAYS==null?"-":result.data[0].SUM_AVGBEDDAYS);//平均住院日-累计值
		 			
		 			$('#'+tabId+' #drugRatio').html(result.data[0].DRUGRATION==null?"-":((result.data[0].DRUGRATION*100).toFixed(2)+"%"));//药占比-本期值
		 			$('#'+tabId+' #drugRatioBenchmark').html(result.data[0].BDRUGCOSTRATIO==null?"-":result.data[0].BDRUGCOSTRATIO);//药占比-目标值
		 			$('#'+tabId+' #drugRatioSum').html(result.data[0].SUM_DRUGRATION==null?"-":result.data[0].SUM_DRUGRATION);//药占比-累计值
		 			
		 			$('#'+tabId+' #caseIn').html(result.data[0].DEPIN==null?"-":"<a href=\"javascript:showCaseDetailDep('"+result.data[0].DR_OUT_DEP+"','in', '转入患者')\">"+result.data[0].DEPIN+"</a>");//转入人数
		 			$('#'+tabId+' #caseOut').html(result.data[0].DEPOUT==null?"-":"<a href=\"javascript:showCaseDetailDep('"+result.data[0].DR_OUT_DEP+"','out', '转出患者')\">"+result.data[0].DEPOUT+"</a>");//转出人数
		 			
		 			$('#'+tabId+' #caseOverTarget').html(result.data[0].OVERBENCHMARK==null?"-":"<a href=\"javascript:showCaseDetailDep('"+result.data[0].DR_OUT_DEP+"','over', '超标病例')\">"+result.data[0].OVERBENCHMARK+"</a>");//超标人数
		 			$('#'+tabId+' #caseNotOverTarget').html(result.data[0].NOTOVER==null?"-":"<a href=\"javascript:showCaseDetailDep('"+result.data[0].DR_OUT_DEP+"','notover', '未超标病例')\">"+result.data[0].NOTOVER+"</a>");//未超标人数
		 			$('#'+tabId+' #response').html(result.data[0].DEPIN==null?"-":'<a href="javascript:depWeekMonitorResp(\''+result.data[0].DEPNAME+'\',\''+result.data[0].BAVGCOST+'\',\''+result.data[0].SUM_AVGCOST+'\',\''+result.data[0].OUTSUM+'\',\''+result.data[0].AVGCOST+'\')"> <i class=\"icon-pencil\"></i></a>');//反馈消息详情
		 		});
		}
	});
	
}

//跳转病例明细
function showCaseDetailDep(depName,type,title) {
	var dateFrom = $('input#startDate').val();
	var dateTo = $('input#endDate').val();
	openTab('/pages/ybHome/changeDep.jsp', title, '转科明细'+depName+'Case'+type, '转科明细'+depName+'Case'+type, {fromDate:dateFrom,toDate:dateTo,depName:depName, type: type, title: title});

}

//标杆悬停
$(window).scroll(onScroll);
function onScroll() {
    var sY = $(window).scrollTop();
    if((sY>54)){
    	$('#'+tabId+' #date_div').css({'position':'fixed','top':'102px','box-shadow':'0 2px 8px #aaa','right':'0px','opacity':'1','transform':'perspective(400px) rotateY(-70deg)','width':'300px'});
    }else if((sY<=54)){
    	$('#'+tabId+' #date_div').css({'position':'','top':'','right':'','box-shadow':'none','opacity':'1','transform':'','width':''});
	}   
}
//展开日期标杆
function opc1(){
	if ($('#'+tabId+' #date_div').css('position')=='fixed') {
		$('#'+tabId+' #date_div').css({'opacity':'1','z-index':'99996','transform':'perspective(400px) rotateY(0deg)'});
	}
}
//收起日期标杆
function opc0(){
	if ($('#'+tabId+' #date_div').css('position')=='fixed') {
		$('#'+tabId+' #date_div').css({'opacity':'1','transform':'perspective(400px) rotateY(-70deg)'});
	}
}


//医保周监测反馈信息
/**
 * depName 科室名称
 * bavgcost 目标值
 * sumavgcost 累计次均费用
 * outsum 出院人数
 * avgcost 次均费用
 */
function depWeekMonitorResp(depName,bavgcost,sumavgcost,outsum,avgcost) {
	var dateFrom = $('input#startDate').val();
	var dateTo = $('input#endDate').val();
	var info=null;
	if(avgcost!=null&&bavgcost!=null){
		if(avgcost-bavgcost>0){
			info = '未达标';
		}else{
			info = '已达标';
		}
	}else{
		info='';
	}	
	var reInfo1="尊敬的"+depName+"科领导:<br>"+"贵科室次均费用目标值为"+bavgcost+"元,科室截止"+dateTo+"累计次均费用为"+sumavgcost+"元<br>"+dateFrom+"日至"+dateTo+
	"日<br>出院人数为："+outsum+"<br>次均费用为："+avgcost+"  "+info+"<br>感谢贵科室对医保办的支持!如需我们帮助,随时联系";
	$('#editDialog .modal-content').css({'width':'400px'});
	$('#editDialog .modal-body').html(reInfo1);
	$('#editDialog').modal('show');
}
